package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.ThreadLocalUtil;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.exception.BusinessException;
import com.sky.mapper.EmployeeMapper;
import com.sky.result.PageResult;
import com.sky.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    //员工登录
    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        //参数接收
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();
        //参数校验
        if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
            throw new BusinessException("非法参数");
        }
        //用户名状态判断
        Employee employee = employeeMapper.getByUsername(username);
        if (employee == null) {
            throw new BusinessException("用户名不存在,请重新输入");
        }
        //密码正确性判断
        String md5 = SecureUtil.md5(password);
        if (!StrUtil.equals(md5, employee.getPassword())) {
            throw new BusinessException("密码错误");
        }
        //状态码判断
        if (employee.getStatus() == StatusConstant.DISABLE) {
            throw new BusinessException("账户被禁用");
        }
        return employee;
    }


    //分页条件查询
    @Override
    public PageResult getPage(EmployeePageQueryDTO employeePageQueryDTO) {

        //开启分页工具
        PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());
        //查询list
        List<Employee> list = employeeMapper.getList(employeePageQueryDTO.getName());
        Page<Employee> page = (Page<Employee>) list;
        return new PageResult(page.getTotal(), page.getResult());
    }

    //新增员工
    @Override
    public void insert(EmployeeDTO employeeDTO) {
        //参数校验
        if (
                StrUtil.isBlank(employeeDTO.getUsername()) ||
                        StrUtil.isBlank(employeeDTO.getName()) ||
                        StrUtil.isBlank(employeeDTO.getPhone()) ||
                        StrUtil.isBlank(employeeDTO.getIdNumber())
        ) {
            throw new BusinessException("非法参数");
        }
        //账号唯一性校验
        if (employeeMapper.getByUsername(employeeDTO.getUsername()) != null) {
            throw new BusinessException("账号已存在");
        }
        //手机号唯一性校验
        if (employeeMapper.getByPhone(employeeDTO.getPhone()) != null) {
            throw new BusinessException("手机号已存在");
        }
        //身份证唯一性校验
        if(employeeMapper.getByIdNumber(employeeDTO.getIdNumber())!=null){
            throw  new BusinessException("身份证号已存在");
        }
        //dto->entity
        Employee employee = BeanUtil.copyProperties(employeeDTO, Employee.class);
        //补全信息
        String md5 = SecureUtil.md5(PasswordConstant.DEFAULT_PASSWORD);
        employee.setPassword(md5);
        employee.setStatus(StatusConstant.ENABLE);
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateUser(ThreadLocalUtil.getCurrentId());
        employee.setUpdateUser(ThreadLocalUtil.getCurrentId());
        //保存到数据库
        employeeMapper.insert(employee);


    }

    // 回显员工
    @Override
    public Employee getById(Long id) {
        return employeeMapper.getById(id);
    }

    // 修改员工
    @Override
    public void updateById(EmployeeDTO employeeDTO) {
        // 1.非法参数校验
        if (employeeDTO.getId()==null) {
            throw new BusinessException("修改id不能为空");
        }
        // 3.dto->entity
        Employee employee = BeanUtil.copyProperties(employeeDTO, Employee.class);
        // 3-1 补充信息
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(ThreadLocalUtil.getCurrentId());

        // 4.调用mapper修改
        employeeMapper.updateById(employee);
    }

    // 启动禁用
    @Override
    public void startOrStop(Integer status, Long id) {
        // 1.封装employee对象
        Employee employee = Employee.builder()
                .id(id)
                .status(status).build();
        // 2.补充信息
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(ThreadLocalUtil.getCurrentId());
        // 3.调用mapper修改
        employeeMapper.updateById(employee);
    }


}
